//
//  SecondViewController.m
//  UI16_UICollectionView
//
//  Created by dllo on 16/3/30.
//  Copyright © 2016年 付冬梅. All rights reserved.
//

#import "SecondViewController.h"

@interface SecondViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) MovieCollectionView *detailMovieCollectionView;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self createSmallView];
    [self createCollectionView];
    [self createRightItem];
}

- (void)createRightItem {
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"xia"] landscapeImagePhone:[UIImage imageNamed:@"shang"] style:UIBarButtonItemStylePlain target:self action:@selector(rightItem:)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    
}

- (void)rightItem:(UIBarButtonItem *)rightItemAction {
    
}

- (void)createNSNotificationCenter {
//    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
}

- (void)createSmallView {
    UIView *smallView = [[UIView alloc] initWithFrame:CGRectMake(0, 40, ScreenWidth / _movieModelArr.count, 4)];
    smallView.backgroundColor = [UIColor greenColor];
    [self.navigationController.navigationBar addSubview:smallView];
    
}

- (void)createCollectionView {
    UICollectionViewFlowLayout *detailLayout = [[UICollectionViewFlowLayout alloc] init];
    detailLayout.minimumInteritemSpacing = 0;
    detailLayout.minimumLineSpacing = 0;
    detailLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    detailLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    detailLayout.itemSize = CGSizeMake(ScreenWidth, 240);
    self.detailMovieCollectionView = [[MovieCollectionView alloc] initWithFrame:CGRectMake(0, 200, ScreenWidth, 260) collectionViewLayout:detailLayout];
    self.detailMovieCollectionView.dataSource = self;
    self.detailMovieCollectionView.delegate = self;
    self.detailMovieCollectionView.pagingEnabled = YES;
    self.detailMovieCollectionView.showsHorizontalScrollIndicator = NO;
    self.detailMovieCollectionView.bounces = NO;
    self.detailMovieCollectionView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.detailMovieCollectionView];
    [self.detailMovieCollectionView registerClass:[MovieCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    /** 当前偏移位置 */
    self.detailMovieCollectionView.contentOffset = CGPointMake(_firstIndexPath.row * ScreenWidth, 0);
    /** 当前选中哪个item */
//    [self.detailMovieCollectionView cellForItemAtIndexPath:_firstIndexPath];
//    NSLog(@"%@", _firstIndexPath);
//    [self.detailMovieCollectionView selectItemAtIndexPath:_firstIndexPath animated:YES scrollPosition:UICollectionViewScrollPositionTop];
//    [self.detailMovieCollectionView scrollToItemAtIndexPath:_firstIndexPath atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"scroll" object:self.detailMovieCollectionView userInfo:nil];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _movieModelArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MovieCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    movieModel *model = _movieModelArr[indexPath.row];
    [cell setMovieModel:model];
    cell.backgroundColor = [UIColor purpleColor];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    MovieCollectionViewCell *cell = (MovieCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor grayColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
